<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="投诉单号" prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="投诉人电话" prop="applyPhone">
        <el-input
          v-model="queryParams.applyPhone"
          placeholder="请输入投诉人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="applyCategory">
        <el-select v-model="queryParams.applyCategory" placeholder="请选择分类" clearable>
          <el-option
            v-for="dict in dict.type.complaints_category"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商户编号" prop="applyHouse">
        <el-input
          v-model="queryParams.applyHouse"
          placeholder="请输入申请的商户编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单状态" prop="complaintsState">
        <el-select v-model="queryParams.complaintsState" placeholder="请选择工单状态" clearable>
          <el-option
            v-for="dict in dict.type.complaints_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户评价" prop="ownerScore">
        <el-select v-model="queryParams.ownerScore" placeholder="请选择客户评价" clearable>
          <el-option
            v-for="dict in dict.type.return_result"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="回访状态" prop="returnState">
        <el-select v-model="queryParams.returnState" placeholder="请选择回访状态" clearable>
          <el-option
            v-for="dict in dict.type.return_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="回访结果" prop="returnResult">
        <el-select v-model="queryParams.returnResult" placeholder="请选择回访结果" clearable>
          <el-option
            v-for="dict in dict.type.return_result"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="数据来源" prop="client">
        <el-select v-model="queryParams.client" placeholder="请选择数据来源" clearable>
          <el-option
            v-for="dict in dict.type.flow_client"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['oa:complaints:add']"
        >新增投诉
        </el-button>
        <el-button
          type="warning"
          icon="el-icon-right"
          size="mini"
          v-if="currentRow.complaintsState!=undefined&&currentRow.complaintsState=='apply'"
          @click="handleApply"
        >受理投诉
        </el-button>
        <el-button
          type="danger"
          icon="el-icon-right"
          size="mini"
          v-if="currentRow.complaintsState!=undefined&&currentRow.complaintsState=='processing'"
          @click="handleComplaint"
        >处理投诉
        </el-button>
        <el-button
          type="warning"
          icon="el-icon-right"
          size="mini"
          v-if="currentRow.complaintsState!=undefined&&currentRow.complaintsState=='score'"
          @click="handleScore"
        >客户评价
        </el-button>
        <el-button
          type="danger"
          icon="el-icon-s-operation"
          size="mini"
          v-if="currentRow.returnState!=undefined&&currentRow.returnState=='un_visit'"
          @click="handleVisit"
        >投诉回访
        </el-button>
        <el-button
          type="warning"
          icon="el-icon-circle-close"
          size="mini"
          v-if="currentRow.complaintsState!=undefined&&currentRow.complaintsState!='complete'&&currentRow.complaintsState!='cancel'"
          @click="handleCancel"
        >撤销投诉
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="complaintsList" highlight-current-row
              @current-change="handleCurrentChange">
      <el-table-column label="单号" align="center" prop="no" width="180"/>
      <el-table-column label="标题" align="center" prop="title" width="180"/>
      <el-table-column label="投诉人电话" align="center" prop="applyPhone" width="120"/>
      <el-table-column label="投诉人姓名" align="center" prop="applyName" width="120"/>
      <el-table-column label="分类" align="center" prop="applyCategory">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.complaints_category" :value="scope.row.applyCategory"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="complaintsState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.complaints_state" :value="scope.row.complaintsState"/>
        </template>
      </el-table-column>
      <el-table-column label="投诉内容" align="center" prop="applyContent" width="120"/>
      <el-table-column label="投诉人的诉求" align="center" prop="applyRequirements" width="120"/>
      <el-table-column label="申请的商户编号" align="center" prop="applyHouse" width="120"/>
      <el-table-column label="申请时间" align="center" prop="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="回访状态" align="center" prop="returnState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.return_state" :value="scope.row.returnState"/>
        </template>
      </el-table-column>
      <el-table-column label="工单结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="总耗时" align="center" prop="timeConsum"/>
      <el-table-column label="数据来源" align="center" prop="client">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.flow_client" :value="scope.row.client"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['oa:complaints:edit']"
          >查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改投诉工单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="数据来源" prop="client">
          <el-radio-group v-model="form.client" size="medium">
            <el-radio-button v-for="dict in dict.type.flow_client" :key="dict.id" :label="dict.value">{{
                dict.label
              }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="分类" prop="applyCategory">
          <el-select style="width: 100%" v-model="form.applyCategory" placeholder="请选择分类">
            <el-option
              v-for="dict in dict.type.complaints_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商户编号" prop="applyHouse" v-if="form.client != 'web_company'">
          <el-select v-model="form.applyHouse" style="width: 100%" filterable placeholder="请选择">
            <el-option
              v-for="item in houseList"
              :key="item.name"
              :label="item.name"
              :value="item.name">
              <span style="float: left">{{ item.name }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.stateCompany }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="投诉人电话" prop="applyPhone">
          <el-input v-model="form.applyPhone" placeholder="请输入投诉人电话"/>
        </el-form-item>
        <el-form-item label="投诉人姓名" prop="applyName">
          <el-input v-model="form.applyName" placeholder="请输入投诉人姓名"/>
        </el-form-item>
        <el-form-item label="投诉内容" prop="applyContent">
          <el-input v-model="form.applyContent" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="投诉人的诉求" prop="applyRequirements">
          <el-input v-model="form.applyRequirements" type="textarea" placeholder="请输入投诉人的诉求"/>
        </el-form-item>
        <el-form-item label="图片" prop="applyImg">
          <imageUpload v-model="form.applyImg"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--选择用户的组件-->
    <select-system-user ref="selectUser" v-if="showSelectUserDialog" @select="confirmSelectUser"></select-system-user>

    <!--打开受理工单的弹出层-->
    <el-dialog :title="title" :visible.sync="openAcceptComplaintOrder" v-if="openAcceptComplaintOrder" width="1000px"
               append-to-body>
      <complaints-detail :complaints-obj="currentRow" v-if="currentRow.title!=undefined"></complaints-detail>
      <el-card style="margin-top: 5px">
        <el-form ref="acceptComplaintOrderForm" :model="acceptComplaintOrderForm" :rules="rules" label-width="100px">
          <el-form-item label="选择处理人" prop="assigneeUser">
            <el-input v-model="acceptComplaintOrderForm.assigneeUser" style="width: 80%" disabled placeholder="请选择处理人"/>
            <el-button type="primary" style="margin-left: 10px" @click="selectHandleComplaintUser">选 择</el-button>
          </el-form-item>
          <el-form-item label="处理人姓名" prop="assigneeUserName">
            <el-input v-model="acceptComplaintOrderForm.assigneeUserName" disabled placeholder="请选处理人姓名"/>
          </el-form-item>
          <el-form-item label="受理备注" prop="content">
            <el-input type="textarea" v-model="acceptComplaintOrderForm.content" placeholder="请输入受理备注"/>
          </el-form-item>
        </el-form>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmitApply">确 定</el-button>
        <el-button @click="openAcceptComplaintOrder=false">取 消</el-button>
      </div>
    </el-dialog>
    <!--打开处理投诉的弹出层-->
    <el-dialog :title="title" :visible.sync="openHandleComplaintOrder" v-if="openHandleComplaintOrder" width="1000px"
               append-to-body>
      <complaints-detail :complaints-obj="currentRow" v-if="currentRow.title!=undefined"></complaints-detail>
      <el-card style="margin-top: 5px">
        <el-form ref="handleComplaintOrderForm" :model="handleComplaintOrderForm" :rules="rules" label-width="120px">
          <el-form-item label="处理方案" prop="content">
            <el-input type="textarea" v-model="handleComplaintOrderForm.content" placeholder="请输入确认备注"/>
          </el-form-item>
        </el-form>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmitComplaint">确 定</el-button>
        <el-button @click="openHandleComplaintOrder=false">取 消</el-button>
      </div>
    </el-dialog>

    <!--打开客户评价的弹出层-->
    <el-dialog :title="title" :visible.sync="openScoreComplaintOrder" v-if="openScoreComplaintOrder" width="1000px"
               append-to-body>
      <complaints-detail :complaints-obj="currentRow" v-if="currentRow.title!=undefined"></complaints-detail>
      <el-card style="margin-top: 5px">
        <el-form ref="scoreComplaintOrderForm" :model="scoreComplaintOrderForm" :rules="rules" label-width="120px">
          <el-form-item label="满意程度" prop="ownerScore">
            <el-radio-group v-model="scoreComplaintOrderForm.ownerScore" size="medium">
              <el-radio-button v-for="dict in dict.type.return_result" :key="dict.id" :label="dict.value">{{
                  dict.label
                }}
              </el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="评价内容" prop="ownerScoreRemark">
            <el-input type="textarea" v-model="scoreComplaintOrderForm.ownerScoreRemark" placeholder="请输入评价内容"/>
          </el-form-item>
        </el-form>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmitScore">确 定</el-button>
        <el-button @click="openScoreComplaintOrder=false">取 消</el-button>
      </div>
    </el-dialog>

    <!--打开物业回访的弹出层-->
    <el-dialog :title="title" :visible.sync="openVisitComplaintOrder" v-if="openVisitComplaintOrder" width="1000px"
               append-to-body>
      <complaints-detail :complaints-obj="currentRow" v-if="currentRow.title!=undefined"></complaints-detail>
      <el-card style="margin-top: 5px">
        <el-form ref="visitComplaintOrderForm" :model="visitComplaintOrderForm" :rules="rules" label-width="120px">
          <el-form-item label="回访结果" prop="returnResult">
            <el-radio-group v-model="visitComplaintOrderForm.returnResult" size="medium">
              <el-radio-button v-for="dict in dict.type.return_result" :key="dict.id" :label="dict.value">{{
                  dict.label
                }}
              </el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="回访意见" prop="returnRemark">
            <el-input type="textarea" v-model="visitComplaintOrderForm.returnRemark" placeholder="请输入评价内容"/>
          </el-form-item>
        </el-form>
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmitVisit">确 定</el-button>
        <el-button @click="openVisitComplaintOrder=false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="showComplaintDetail" v-if="showComplaintDetail" width="1000px"
               append-to-body>
      <complaints-detail :complaints-obj="currentRow" v-if="currentRow.title!=undefined"></complaints-detail>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showComplaintDetail=false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listComplaints, addComplaints, applyComplaints,handleComplaints,doComplaintsScore,doComplaintsVisit,doCancel} from "@/api/oa/complaints";
import {getAllHouses} from "@/api/system/house";
import SelectSystemUser from "@/views/components/selectSystemUser/selectSystemUser"
import ComplaintsDetail from "@/views/oa/complaints/complaints-detail";

export default {
  name: "Complaints",
  components:{
    SelectSystemUser,
    ComplaintsDetail
  },
  dicts: ['return_result', 'complaints_category', 'return_state', 'flow_client','complaints_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 投诉工单表格数据
      complaintsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ownerId: null,
        no: null,
        title: null,
        applyPhone: null,
        applyCategory: null,
        applyHouse: null,
        assigneeUserName: null,
        complaintsState: null,
        ownerScore: null,
        returnState: null,
        returnResult: null,
        client: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        client: [{required: true, message: '请选择报修数据来源', trigger: 'blur'}],
        applyHouse: [{required: true, message: '请选择商铺编号', trigger: 'blur'}],
        applyPhone: [
          {required: true, message: '报修电话不能为空', trigger: 'blur'},
          {pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: '请输入正确的手机号码', trigger: 'blur'},
        ],
        applyName: [{required: true, message: '报修者姓名不能为空', trigger: 'blur'}],
        applyCategory: [{required: true, message: '报修分类不能为空', trigger: 'change'}],
        assigneeUser: [{required: true, message: '处理人ID不能为空', trigger: 'blur'}],
        assigneeUserName: [{required: true, message: '处理人不能为空', trigger: 'blur'}],
        assigneeContent: [{required: true, message: '处理备注不能为空', trigger: 'blur'}],
        ownerScore: [{required: true, message: '客户评价不能为空', trigger: 'blur'}],
        content: [{required: true, message: '备注不能为空', trigger: 'blur'}],
      },
      //house列表
      houseList: [],
      //当前选中行
      currentRow: {},
      //是否打开受理投诉工单弹出层
      openAcceptComplaintOrder: false,
      //受理表单数据
      acceptComplaintOrderForm: {},
      //是否打开选择用户的弹出层
      showSelectUserDialog: false,
      //是否打开处理投诉弹出层
      openHandleComplaintOrder: false,
      //处理投诉的表单数据
      handleComplaintOrderForm: {},
      //是否打开客户评价弹出层
      openScoreComplaintOrder: false,
      //客户评价的表单数据
      scoreComplaintOrderForm: {},
      //是否打开物业回访弹出层
      openVisitComplaintOrder: false,
      //物业回访的表单数据
      visitComplaintOrderForm: {},
      //是否打开详情的弹出层
      showComplaintDetail:false
    };
  },
  created() {
    this.getList();
    //加载所有house
    this.loadAllHouses();
  },
  methods: {
    //加载所有house
    loadAllHouses() {
      getAllHouses().then(res => {
        this.houseList = res.data;
      })
    },
    /** 查询投诉工单列表 */
    getList() {
      this.loading = true;
      listComplaints(this.queryParams).then(response => {
        this.complaintsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        title: null,
        applyPhone: null,
        applyName: null,
        applyCategory: null,
        applyContent: null,
        applyHouse: null,
        applyTime: null,
        applyImg: null,
        appContent: null,
        client: 'web_company',
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.currentRow = {}
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.currentRow = {}
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //单选框选中数据
    handleCurrentChange(val) {
      this.currentRow = val;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加投诉工单";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addComplaints(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 打开受理投诉工单操作 */
    handleApply() {
      this.openAcceptComplaintOrder = true;
      this.title = `受理【${this.currentRow.title}】的投诉工单`
    },
    /** 受理投诉工单提交 */
    handleSubmitApply() {
      this.$refs["acceptComplaintOrderForm"].validate(valid => {
        if (valid) {
          this.acceptComplaintOrderForm.id = this.currentRow.id;
          applyComplaints(this.acceptComplaintOrderForm).then(res => {
            this.$message.success("受理成功")
            this.openAcceptComplaintOrder = false;
            this.handleQuery();
          })
        }
      });
    },
    /** 打开处理投诉操作 */
    handleComplaint() {
      this.openHandleComplaintOrder = true
      this.title = `处理【${this.currentRow.title}】的投诉工单`
    },
    /** 处理投诉操作 */
    handleSubmitComplaint() {
      this.$refs["handleComplaintOrderForm"].validate(valid => {
        if (valid) {
          this.handleComplaintOrderForm.id = this.currentRow.id;
          handleComplaints(this.handleComplaintOrderForm).then(res => {
            this.$message.success("处理投诉成功")
            this.handleQuery();
            this.openHandleComplaintOrder=false
          })
        }
      });
    },
    /** 打开客户评价操作 */
    handleScore() {
      this.openScoreComplaintOrder = true
      this.title = `业主评价【${this.currentRow.title}】的投诉工单`
    },
    /** 客户评价操作 */
    handleSubmitScore() {
      this.$refs["scoreComplaintOrderForm"].validate(valid => {
        if (valid) {
          this.scoreComplaintOrderForm.id = this.currentRow.id;
          doComplaintsScore(this.scoreComplaintOrderForm).then(res => {
            this.$message.success("完成客户评价")
            this.handleQuery();
            this.openScoreComplaintOrder=false
          })
        }
      });
    },
    /** 打开物业回访操作 */
    handleVisit() {
      this.openVisitComplaintOrder = true
      this.title = `物业回访【${this.currentRow.title}】的投诉工单`
    },
    /** 提交物业回访操作 */
    handleSubmitVisit() {
      this.$refs["visitComplaintOrderForm"].validate(valid => {
        if (valid) {
          this.visitComplaintOrderForm.id = this.currentRow.id;
          doComplaintsVisit(this.visitComplaintOrderForm).then(res => {
            this.$message.success("完成物业回访")
            this.handleQuery();
            this.openVisitComplaintOrder=false
          })
        }
      });
    },
    //选择处理员
    selectHandleComplaintUser() {
      this.showSelectUserDialog = true;
      this.$nextTick(_ => {
        this.$refs.selectUser.initSelectUser();
      })
    },
    //选择处理人员人员之后的回调
    confirmSelectUser(user) {
      this.acceptComplaintOrderForm.assigneeUser = user.userId;
      this.acceptComplaintOrderForm.assigneeUserName = user.nickName;
      this.showSelectUserDialog = false;
    },
    //撤销保修
    handleCancel(){
      this.$confirm(`是否确定撤销【${this.currentRow.title}】的投诉单`, '是否继续?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        doCancel(this.currentRow.id).then(res=>{
          this.$message.success("完成撤销")
          this.handleQuery();
        })
      });
    },
    //查看
    handleView(row){
      this.title="投诉工单详情"
      this.showComplaintDetail=true;
      this.$nextTick(_ => {
        this.currentRow=row;
      })
    }
  }
};
</script>
